#include "onemanga.h"

OneManga::OneManga()
{
    m_currentAddress = "";
}
void OneManga::getImage(QString path)
{
    m_progressTotalStep = 3;
    m_currentProgress = 0;
    m_currentAddress = path;
    connect(&m_http, SIGNAL(requestFinished(int,bool)), this, SLOT(on_data_received(int,bool)));
    m_http.setHost("www.onemanga.com");
    emit progressStep(m_currentProgress);
    m_http.get(path);
    emit progressStep(performProgressStep());
}

void OneManga::getMangas()
{
    QHttp * http    = new QHttp();
    connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(on_index_received(int,bool)));
    http->setHost("www.onemanga.com");
    http->get("/");
}

void OneManga::on_data_received(int id, bool error)
{
    QString s(m_http.readAll());
    QString imgsrc,prec,suiv;
    int a,b;
    if(!s.isEmpty())
    {
	a = s.indexOf("<img class=\"manga-page\" src=\"");
	a = s.indexOf("http",a);
	b = s.indexOf("\"",a);
	imgsrc = QString(s);
	imgsrc.truncate(b);
	imgsrc = imgsrc.right(b-a);

	a = s.indexOf("location=",b);
	a = s.indexOf("/",a);
	b = s.indexOf("'",a);

	prec = QString(s);
	prec.truncate(b);
	prec = prec.right(b-a);
	emit previousReady(prec);

	a = s.indexOf("location=",b);
	a = s.indexOf("/",a);
	b = s.indexOf("'",a);

	suiv = QString(s);
	suiv.truncate(b);
	suiv = suiv.right(b-a);
	emit nextReady(suiv);

	disconnect(&m_http, SIGNAL(requestFinished(int,bool)), this, SLOT(on_data_received(int,bool)));
	connect(&m_http, SIGNAL(requestFinished(int,bool)), this, SLOT(on_img_received(int,bool)));
	QUrl url = QUrl(imgsrc);
	m_http.setHost(url.host());
	m_http.get(url.encodedPath());
	emit progressStep(performProgressStep());
    }
}
void OneManga::on_img_received(int id, bool error)
{
    QByteArray b = m_http.readAll();
    if(!b.isEmpty())
    {
	emit progressStep(performProgressStep());
	emit imageReady(b);
	disconnect(&m_http, SIGNAL(requestFinished(int,bool)), this, SLOT(on_img_received(int,bool)));
    }
}

void OneManga::on_index_received(int id, bool error)
{
    if (error)
        return;

    QHttp * http    = (QHttp *)sender();
    QString html(http->readAll());
    if (html.isEmpty())
        return;

    QMap<QString, QString>  mangas  = parseWebComboBox("", html);
    emit mangasReady(mangas);    

    // We don't need the http object anymore, so we free it up.
    http->deleteLater();
}

int OneManga::performProgressStep()
{
    m_currentProgress += 100/m_progressTotalStep;
    return m_currentProgress;
}
QString OneManga::getCurrentAddress()
{
    return m_currentAddress;
}

QMap<QString, QString> OneManga::parseWebComboBox(const QString & boxName, const QString & htmlPage)
{
    QMap<QString, QString> items;
    QRegExp regex("<select[^>]*>(.+)</select>");

    // By default, our regex matches the first web combobox found on the html page. However,
    // the programmer can override this by specifying the name of the desired combobox.
    if (!boxName.isEmpty())
        regex.setPattern(QString("<select[^(name)]+name=\"%1\"[^>]*>(.+)</select>").arg(boxName));

    if (regex.indexIn(htmlPage) == -1)
        return items;

    // Parse the items from the combobox.
    QStringList rawItems = regex.cap(1).split("</option>");
    regex.setPattern("<option value=\"([^\"]+)\"[^>]*>(.+)");

    for (int i = 0; i < rawItems.length(); i++)
    {
        // Does the item have a name and a value?
        if (regex.indexIn(rawItems.at(i)) == -1)
            continue;

        QString itemValue = regex.cap(1);
        QString itemName = regex.cap(2);
        items.insert(itemName, itemValue);
    }

    return items;
}
